<template>
  <el-row class="main-container">
    <!-- 左侧ContentWrap -->
    <el-col :span="2" class="content-wrap left-content">
      <!-- 第一个ContentWrap的内容 -->
        <ContentWrap>
        
        <!-- 使用el-menu展示一级分类 -->
        <el-menu
          :default-active="activeIndex"
          class="el-menu-vertical-demo"
          @select="handleSelect"
        >
          <el-menu-item index="">全部</el-menu-item>
          <el-menu-item
            v-for="item in listSort"
            :key="item.id"
            :index="item.id.toString()"
          >
            {{ item.name }}
          </el-menu-item>
        </el-menu>
      </ContentWrap>
    </el-col>

    <!-- 右侧ContentWrap，分为上下两部分 -->
    <el-col :span="22" class="content-wrap right-content">
      <!-- 上部分：搜索工作栏 -->
      <el-row class="search-bar-container">
        <el-col :span="24">
          <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <!-- <el-form-item label="分类" prop="pictureSortId"  class="!w-240px"> 
        <el-select v-model="queryParams.pictureSortId">
          <el-option
            v-for="option in listSort"
            :key="option.id"
            :label="option.name"
            :value="option.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item>
        <!-- <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button> -->
        
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['ai:picture:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 记录作品
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['ai:picture:create']"
        >
          <Icon icon="ep:select" class="mr-5px" /> 选择
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['ai:picture:create']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 删除
        </el-button>
        <!-- <el-button
          type="primary"
          plain
          @click="openSingleForm('createSingle')"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 记录关键词
        </el-button> -->
        <!-- <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['ai:picture:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>
        </el-col>
      </el-row>

      <!-- 下部分：图片列表和分页 -->
  <el-row class="list-and-pagination-container">
    <el-col :span="24">
    <ContentWrap>

      <div class="image-container">
        <el-row :gutter="20">
          <el-col :span="3" v-for="(item, index) in list" :key="index" class="image-col">
            <div class="image-wrapper">
                <img
                  :src="item.fileId"
                  class="image-item"
                  v-viewer
                />
                  <!-- 编辑和删除按钮 -->
                <div class="edit-delete-btns">
        <Icon
          icon="ep:edit"
          @click="openForm('update', item.id)"
          v-hasPermi="['ai:picture:update']"
          class="edit-icon"
        />
        <Icon
          icon="ep:delete"
          @click="handleDelete(item.id)"
          v-hasPermi="['ai:picture:delete']"
          class="delete-icon"
        />
      </div>
              </div>
          </el-col>
        </el-row>
      </div>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
        </el-col>
      </el-row>
    </el-col>
  </el-row>

  <!-- 表单弹窗：添加/修改 -->
  <PictureForm ref="formRef" @success="getList" />

  <!-- 表单弹窗：添加/修改 -->
  <PictureSingleForm ref="formSingleRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { PictureApi, PictureVO } from '@/api/ai/picture'
import PictureForm from './PictureForm.vue'
import PictureSingleForm from './PictureSingleForm.vue'
import { PictureSortApi, PictureSortVO } from '@/api/ai/picturesort'

/** 图片 列表 */
defineOptions({ name: 'Picture' })


const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<PictureVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数

const listSort = ref<PictureSortVO[]>([]) // 图片分类列表的数据
const totalSort = ref(0) // 图片分类列表的总页数

// 活动菜单项的索引
const activeIndex = ref('0'); // 假设 '1' 是默认选中的分类 ID

// “全部”分类的初始项
const initialSort = [{ id: '', name: '全部' }];


const querySortParams = reactive({
  pageNo: 1,
  pageSize: 100,
})

const queryParams = reactive({
  pageNo: 1,
  pageSize: 20,
  picName: undefined,
  pictureSortId: undefined as string | undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中


// 菜单项被点击时的处理函数
const handleSelect = (index: string, item: any) => {
  // 这里可以根据实际需要进行处理，比如根据分类 ID 查询相关图片等
  queryParams.pictureSortId = index
  queryParams.pageNo = 1
  getList()

};



/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    console.info("queryParams",queryParams);
    const data = await PictureApi.getPicturePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 关键词添加/修改操作 */
const formSingleRef = ref()
const openSingleForm = (type: string, id?: number) => {
  formSingleRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm();
    // 发起删除
    await PictureApi.deletePicture(id);
    message.success(t('common.delSuccess'));
    // 刷新列表
    await getList();
  } catch {}
};

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PictureApi.exportPicture(queryParams)
    download.excel(data, '图片.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList();
  getSortList();
})



const getSortList = async () => {
    const sortData = await PictureSortApi.getPictureSortPage(querySortParams)
    listSort.value = sortData.list
    totalSort.value = sortData.total
}



</script>
<style scoped>
.image-container {
  margin: 10px;
}

.image-col {
  margin-bottom: 10px;
}

.image-wrapper {
  position: relative;
  cursor: pointer;
}

.image-item {
  width: 100%;
  height: 220px;
  transition: transform 0.3s ease;
  display: block; /* 确保img标签是块级元素 */
}

.image-wrapper:hover .image-item {
  transform: scale(1.05);
}


.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}


.image-wrapper {
  position: relative;
  display: inline-block;
  cursor: pointer;
}

/* 添加到现有的 <style> 标签中 */
.image-wrapper:hover .edit-delete-btns {
  visibility: visible;
  opacity: 1;
}

.edit-delete-btns {
  position: absolute;
  bottom: 10px;
  right: 10px;
  visibility: hidden;
  opacity: 0;
  transition: visibility 0.3s, opacity 0.3s ease;
  z-index: 10;
}

.edit-icon,
.delete-icon {
  cursor: pointer;
  padding: 5px; /* 根据需要调整图标外边距 */
  color: #c2f50c; /* 图标默认颜色 */
  transition: color 0.3s, transform 0.3s;
}

.edit-icon:hover {
  color: #ac071d; /* 鼠标悬浮时编辑图标的颜色 */
  transform: scale(1.1); /* 鼠标悬浮时图标放大 */
}

.delete-icon:hover {
  color: #f56c6c; /* 鼠标悬浮时删除图标的颜色 */
  transform: scale(1.1); /* 鼠标悬浮时图标放大 */
}

</style>